﻿#region - Class details -
/*  
 *  Developer   :Tony Hicks
 *  Description : 
 *  History     :
 */
#endregion

using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace QuickApp
{
    public class SqlServerManager
    {
        #region Private Variables
        private static readonly SqlServerManager instance = new SqlServerManager();
        private static string connString = "";
        #endregion

        #region Constructor
        private SqlServerManager()
        {
        }
        #endregion

        #region Public Properties
        public static SqlServerManager Instance
        {
            get { return SqlServerManager.instance; }
        } 
        #endregion

        #region - Private methods -
        private object getCommand(string command, CommandType cmd)
        {
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = connString;
            conn.Open();
            SqlCommand exe = conn.CreateCommand();
            exe.CommandText = command;
            switch (cmd)
            {
                case CommandType.SelectDataTable :
                    DataTable dt = new DataTable();
                    dt.Load(exe.ExecuteReader());
                    return dt;
                case CommandType.SelectScalar:
                    return exe.ExecuteScalar();
                case CommandType.UpdateInsert:
                    return exe.ExecuteNonQuery();
            }
            return null;
        }
        #endregion

        #region - Public methods -
        public bool SetConnection(string connectionString)
        {
            connString = connectionString;
            return CanConnect();
        }
        public bool SetConnection(string server, string database, string user, string password)
        {
            connString = string.Format("Data Source={0};Initial Catalog={1};User Id={2};Password={3};",server,database,user,password);
            return CanConnect();
        }
        public bool CanConnect()
        {
            SqlConnection conn = new SqlConnection();
            try
            {
                conn.ConnectionString = connString;
                conn.Open();
                return true;
            }
            catch (SqlException)
            {
                return false;
            }
            finally
            {
                conn.Close();
            }
        }
        public DataTable SelectTable(string command)
        {
            if (connString.Length > 5)
                return (DataTable)getCommand(command, CommandType.SelectDataTable);
            return null;
        }
        public object SelectScalar(string command)
        {
            if (connString.Length > 5)
                return getCommand(command, CommandType.SelectScalar);
            return null;
        }
        public int Update(string command)
        {
            if (connString.Length > 5)
                return (int)getCommand(command, CommandType.UpdateInsert);
            return 0;
        }
        public int Insert(string command)
        {
            return Update(command);
        }
        public int Delete(string command)
        {
            return Update(command);
        }
        #endregion

        #region Enums
        private enum CommandType:int
        {
            UpdateInsert = 1, 
            SelectDataTable = 2,
            SelectScalar = 3
        }
        #endregion
    }
}
